package com.dish.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dish.bean.CartItemBean;
import com.dish.bean.Dish;
import com.dish.dao.DishDAO;
import com.dish.dao.DishDAOImpl;
import com.dish.utils.DAOFactory;

@WebServlet("/addtocart")
public class AddToCart extends HttpServlet {
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		//根据传递过来的dishid，从数据库中找到所有的dish，然后找到与dishid匹配的一个
		ArrayList<Dish> list = new ArrayList<>();
		DishDAOImpl dishDAOImpl = (DishDAOImpl) DAOFactory.newInstance("DishDAO");
		String dishId = request.getParameter("dishid");
		list = dishDAOImpl.findDishes();
		Dish dishtocart = null;
		for(Dish dish:list)
		{
			if (dish.getDishid() == Integer.valueOf(dishId)) {
				dishtocart = dish;
				break;
			}
		}
		
		//购物车中的信息保存在session中
		HttpSession session = request.getSession(false);
		@SuppressWarnings("unchecked")
		Map<Integer, CartItemBean> cart = (Map<Integer, CartItemBean>) session.getAttribute("cart");
		//session中没有cart这个Map时
	    if (cart == null) {
	          cart = new HashMap<Integer, CartItemBean>();
//	          session.setAttribute("cart", cart)
	          cart.put(dishtocart.getDishid(), new CartItemBean(dishtocart, 1));
	    }else{   //	session中有cart这个Map时
			CartItemBean cartItem = (CartItemBean) cart.get(dishtocart.getDishid());	
		    if (cartItem != null){  //cart中有当前的dish时，使对应数量加1
		        cartItem.setQuantity(cartItem.getQuantity() + 1);
		    } else{  //cart中没有当前的dish时，新建一组key、value
		    	cart.put(dishtocart.getDishid(), new CartItemBean(dishtocart, 1));
		    }	
	    }
	    
	    int totalNum = 0;  //购物车中的商品数量
		if (cart == null) {
	          cart = new HashMap<Integer, CartItemBean>();
	          totalNum = 0;
	    }else{
	    	for(Integer key:cart.keySet())
	    	{
	    		totalNum = totalNum + cart.get(key).getQuantity();
	    	}
	    }
		session.setAttribute("totalNum", totalNum);
	    session.setAttribute("cart", cart);
	    //out.println("添加成功！");
//		out.close();
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/NewShowDish");
		requestDispatcher.forward(request, response);
		return;
	}



	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
